package com.tempjob.app.controller;

import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.model.PageParam;
import com.tempjob.app.dto.IdQueryDTO;
import com.tempjob.app.dto.PolicyApplyDTO;
import com.tempjob.app.dto.UserPolicyApplyDTO;
import com.tempjob.app.security.annotation.UserLogAnnotation;
import com.tempjob.app.security.annotation.UserPassportSSO;
import com.tempjob.common.businesss.mongo.entity.PolicyApplyRecord;
import com.tempjob.common.businesss.mongo.entity.PolicyContent;
import com.tempjob.common.businesss.service.MongoBeanUtil;
import com.tempjob.common.context.UserContextManage;
import com.tempjob.common.model.BaseSearchPageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author hui
 **/
@RequestMapping("/policy")
@RestController
@Api(tags = "政策相关接口")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
@UserPassportSSO
public class PolicyController {

    @Autowired
    private MongoBeanUtil mongoBeanUtil;

    @ApiOperation(response = PolicyContent.class, value = "政策列表", notes = "")
    @PostMapping(value = "/policyList")
    @UserLogAnnotation(title = "政策接口", action = "政策列表")
    @UserPassportSSO(ignore = true)
    public Object policyList(@RequestBody BaseSearchPageDTO dto) {
        PageParam page = new PageParam(dto.getPage(), dto.getSize());
        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getPolicyContentService().lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("policy_time");
        }
        return mongoBeanUtil.getPolicyContentService().page(queryWrapper, page);
    }

    @ApiOperation(response = PolicyContent.class, value = "政策详情", notes = "")
    @PostMapping(value = "/policyDetail")
    @UserLogAnnotation(title = "政策接口", action = "政策详情")
    @UserPassportSSO(ignore = true)
    public Object policyDetail(@RequestBody IdQueryDTO dto) {
        return mongoBeanUtil.getPolicyContentService().getById(dto.getId());
    }


    @ApiOperation(response = Boolean.class, value = "政策申请", notes = "")
    @PostMapping(value = "/policyApply")
    @Transactional
    @UserLogAnnotation(title = "政策接口", action = "政策申请")
    public Object policyApply(@RequestBody PolicyApplyDTO dto) {
        PolicyApplyRecord record = new PolicyApplyRecord();
        record.setPolicyId(dto.getPolicyId());
        record.setUserDesc(dto.getUserDesc());
        record.setApplyUserId(UserContextManage.getContext().getId());
        record.setApplyTime(new Date());
        record.setApplyStatus(0);
        record.setOperTime(new Date());
        record.setCreateTime(new Date());
        return mongoBeanUtil.getPolicyApplyRecordService().save(record);
    }


    @ApiOperation(response = PolicyApplyRecord.class, value = "政策申请列表", notes = "")
    @PostMapping(value = "/policyApplyPage")
    @Transactional
    @UserLogAnnotation(title = "政策接口", action = "政策申请列表")
    public Object policyApplyPage(@RequestBody UserPolicyApplyDTO dto) {
        PageParam page = new PageParam(dto.getPage(), dto.getSize());
        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getPolicyApplyRecordService().lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("apply_time");
        }
        queryWrapper.eq(StringUtils.isNotBlank(dto.getPolicyId()), "policy_id", dto.getPolicyId());
        queryWrapper.eq(dto.getApplyStatus() != null, "apply_status", dto.getApplyStatus());
        queryWrapper.eq("apply_user_id", UserContextManage.getContext().getId());
        return mongoBeanUtil.getPolicyApplyRecordService().page(queryWrapper, page);
    }

}
